package com.yjx.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.Query;
import com.yjx.gulimall.product.dao.CategoryDao;
import com.yjx.gulimall.product.entity.CategoryEntity;
import com.yjx.gulimall.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //查询所有分类
        List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);

        //组装成父子的树形结构
        List<CategoryEntity> collect = categoryEntityList.stream()
                .filter(menu -> menu.getParentCid().equals(0L))
                .map(menu -> {
                    //获取子菜单
                    menu.setChildren(getChildren(menu, categoryEntityList));
                    return menu;
                })
                .sorted(Comparator.comparingInt(CategoryEntity::getSort))
                .collect(Collectors.toList());

        return collect;
    }

    @Override
    public void removeMenuByIds(List<Long> list) {

        //TODO 1、检查菜单是否被其他地方引用

        //批量删除菜单
        baseMapper.deleteBatchIds(list);
    }

    /**
     * 遍历获取子菜单
     *
     * @param root
     * @param categoryEntityList
     * @return
     */
    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> categoryEntityList) {

        return categoryEntityList.stream()
                .filter(categoryEntity -> root.getCatId().equals(categoryEntity.getParentCid()))
                .map(categoryEntity -> {
                    //递归子菜单
                    categoryEntity.setChildren(getChildren(categoryEntity, categoryEntityList));
                    return categoryEntity;
                }).sorted(Comparator.comparingInt(CategoryEntity::getSort))
                .collect(Collectors.toList());
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {

        List<Long> paths = new ArrayList<>();

        List<Long> parentId = findParentId(catelogId, paths);

        Collections.reverse(parentId);

        return paths.toArray(new Long[paths.size()]);
    }

    /**
     * 获取父节点id
     * @param categoryId
     * @param paths
     * @return
     */
    private List<Long> findParentId(Long categoryId,List<Long> paths){
        //收集当前节点的id
        paths.add(categoryId);
        //获取父节点id
        CategoryEntity categoryEntity = this.getById(categoryId);
        if(categoryEntity.getParentCid() != 0){
            paths = findParentId(categoryEntity.getParentCid(),paths);
        }
        return paths;
    }


}